/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EventComponent;

import Bean.Account.AccountSC;
import Bean.Others.EventBean;
import DB.DBConnection;
import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author DIT-0958372
 */
public class EventLogic {

    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;
    private EventBean eb;

    protected boolean createEventDB(EventBean eb) {
        boolean isDone = false;
        String sql = "";

        try {
            //get DB connection
            DBConnection db = new DBConnection();
            conn = db.getConnection();
//            call stored procedure
            sql = "call createEvent(?,?,?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
//              update databse
            //pstmt.setInt(1, eb.getEventIndex());
            pstmt.setString(2, eb.getEventName());
            pstmt.setString(3, eb.getEventDescription());
            pstmt.setString(4, eb.getEventCreator());
            pstmt.setString(5, eb.getStartTime());
            pstmt.setString(6, eb.getEndTime());
            pstmt.setInt(7, eb.getNoOfParticipants());

            int check = pstmt.executeUpdate();

            if (check == 1) {
                isDone = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
        return isDone;
    }

    // the list method is needed to be redone in order to fit the new db design
    // to get the events i have joined from the databse
    protected ArrayList eventIJoinedList(String tblName, String acName, boolean isHost) {
        ArrayList eventList = new ArrayList();
        EventBean eb = null;
        //get DB connection
        DBConnection db = new DBConnection();
        conn = db.getConnection();
        String sql = "";
        int eventIndex = 0;
        try {
            if (isHost == true) {
                sql = "call getEventIJoined(?)";
            } else {
                sql = "call getEventIOrganized(?)";
            }

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, acName);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                eb = new EventBean();
                eventIndex = rs.getInt("eventIndex");
                eb.setEventIndex(eventIndex);
                eb.setEventName(rs.getString("eventName"));
                eb.setEventDescription(rs.getString("eventDescription"));
                
                //set the host list arraylist for the event
                eb.setEventHostList(this.genNameList(eventIndex, true));
                //set the p list arraylist for the event, used to display the event details
                eb.setEventParticipantsList(this.genNameList(eventIndex, false));

                eb.setEventParticipantsList(this.genNameList(rs.getString("eventParticipants")));
                eb.setNoOfParticipants(rs.getInt("noOfParticipants"));
                eb.setStartTime(rs.getString("startTime"));
                eb.setEndTime(rs.getString("endTime"));
                eb.setEventCreator(rs.getString("eventCreator"));

                eventList.add(eb);
            }


        } catch (Exception e) {
        }
        return eventList;
    }

    //Name list for both the participants or the organizers for an event
    protected ArrayList genNameList(int eventIndex, boolean isHost) {
        ArrayList nameList = new ArrayList();
        AccountSC users = null;
        String sql = "";
        //get DB connection
        DBConnection db = new DBConnection();
        conn = db.getConnection();

        try {
            if (isHost) {
                sql = "call getHostList(?)";
            } else {
                sql = "call getParticipantsList(?)";
            }

            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, eventIndex);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                users = new AccountSC();

                users.setAcType(rs.getString("acType"));
                users.setAcName(rs.getString("acName"));
                users.setAcName(rs.getString("acName"));

                nameList.add(users);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nameList;
    }

    private ArrayList genNameList(String nameStr) {
        String[] strList = nameStr.split("/");
        ArrayList nameList = new ArrayList();
        for (int i = 0; i < strList.length; i++) {
            nameList.add(strList[i]);
        }
        return nameList;
    }
    // in progress, need to change a lot
    protected boolean updateEvent(String condition, int eventIndex, String listToChange) {
        boolean isDone = false;
        String sql = "";
        String listStr = "";
        try {
            //get DB connection
            DBConnection db = new DBConnection();
            conn = db.getConnection();

//            for (int i = 0; i < listToChange.size(); i++) {
//                listStr = listToChange.get(i).toString() + "/";
//            }

            if (condition.equals("eventHost")) {

                sql = "call updateHostList(" + eventIndex + "," + listStr + ")";

            } else if (condition.equals("eventParticipants")) {

                sql = "call updateParticipantsList(" + eventIndex + "," + listStr + ")";

            } else if (condition.equals("modifyEvent")) {
                sql = "";
            }
            //          if the condition is empty, the whole table will be selected

            pstmt = conn.prepareStatement(sql);

            int check = pstmt.executeUpdate();

            if (check == 1) {
                isDone = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.close();
        }

        return isDone;
    }

    private int genNewIndex() {
        int index = 0;
        try {
            //get DB connection
            DBConnection db = new DBConnection();
            conn = db.getConnection();

            String sql = "select eventIndex from tblEvent order by eventIndex desc limit 1";
            pstmt = conn.prepareStatement(sql);

//            pstmt.setString(1,"tblEvent");
//            pstmt.setString(2,"eventIndex");

            rs = pstmt.executeQuery();

            if (rs.next()) {
                index = rs.getInt("eventIndex");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return index + 1;
    }

    protected void close() {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
